﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion

using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Windows.Threading;
using PentaBiz.ComponentModel;
using PentaBiz.Studio.Plugins.MetaServer.Services;

namespace PentaBiz.Studio.Plugins.MetaServer.ViewModels
{
    internal class MetaServerPluginModel : DispatcherModel
    {
        private bool _canStart;
        private bool _canStop;
        private bool _isStarted;
        private MetaCommandPluginService _metaCommandPluginService;
        private MetaDbQueryPluginService _metaDbQueryPluginService;
        private int _port;
        private string _serviceUserName;

        public MetaServerPluginModel()
        {
            _metaCommandPluginService = MetaCommandPluginService.Instance;
            _metaDbQueryPluginService = MetaDbQueryPluginService.Instance;


            _canStart = true;
            _serviceUserName = Environment.UserName;
            _port = 8443;
        }

        public string ServiceUserName
        {
            get { return _serviceUserName; }
        }

        public int Port
        {
            get { return _port; }
        }

        public bool IsStarted
        {
            get { return _isStarted; }
            set { _isStarted = value; }
        }

        public Uri MetaCommandServiceUri { get; private set; }
        public Uri MetaDataQueryServiceUri { get; private set; }


        [Display(AutoGenerateField = false)]
        public bool CanStart
        {
            get { return _canStart; }
            private set { _canStart = value; }
        }

        [Display(AutoGenerateField = false)]
        public bool CanStop
        {
            get { return _canStop; }
            private set { _canStop = value; }
        }

        public void Start()
        {
            CanStart = false;
            Dispatcher.BeginInvoke(() =>
                {
                    _metaCommandPluginService.Start();
                    _metaDbQueryPluginService.Start();
                    IsStarted = true;
                    MetaCommandServiceUri = MetaCommandPluginService.Instance.GetMetaCommandService().Address;
                    MetaDataQueryServiceUri = MetaDbQueryPluginService.Instance.GetMetaDbQueryServiceHost().Address;

                    CanStop = true;
                });
        }

        public void Stop()
        {
            CanStop = true;

            Dispatcher.BeginInvoke(() =>
                {
                    _metaCommandPluginService.Stop();
                    _metaDbQueryPluginService.Stop();

                    IsStarted = false;
                    MetaCommandServiceUri = null;
                    MetaDataQueryServiceUri = null;

                    CanStart = true;
                });
        }


        public void GrantPortAccess()
        {
            Dispatcher.BeginInvoke(() =>
                {
                    var p = new Process
                        {
                            StartInfo =
                                {
                                    FileName = "netsh",
                                    Verb = "runas",
                                    Arguments = "http add urlacl url=http://+:8443/ user=" + ServiceUserName
                                }
                        };
                    p.Start();

                    p.WaitForExit((int) TimeSpan.FromSeconds(30).TotalMilliseconds);
                }, DispatcherPriority.ApplicationIdle
                );
        }
    }
}